import {Component, OnInit, ViewEncapsulation, OnDestroy, ViewChild} from '@angular/core';
import {MineService} from "@mine/mine.service";
import {Subject} from "rxjs";

import {ToolsService} from '@shared/service/tools/tools.service';

import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";

@Component({
  selector: 'seed-collect',
  templateUrl: './collect.component.html',
  styleUrls: ['./collect.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class CollectComponent implements OnInit , OnDestroy {
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public collectData= {
    favoriteType: 3,
    page: 1,
    row: 10
  };
  public hasData= true;
  public collectList = [];
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  constructor(
    public mineSer: MineService,
    public tools: ToolsService,
  ) {
  }

  getCollectList() {
    this.tools.showLoading();
    this.mineSer.getUserFavoriteList(this.collectData).subscribe((res) => {
      this.infiniteScroll.complete();
      if (res.result === '0') {
        if (res.data.list.length === 0) {
          this.hasData = false;
        }else {
          this.collectData.page++;
          this.collectList = [...this.collectList, ...res.data.list];
          if (!res.data.hasNextPage) {
            this.infiniteScroll.enable(false);
          }
        }
      }
      this.tools.hideLoading();
    } );
  }
  ngOnInit() {
    this.getCollectList();
  }
  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
